﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Liquid.Collections;

namespace GapBufferTests {
	[TestClass]
	public class Resizing {
		public TestContext TestContext { get; set; }

		[TestMethod]
		public void FillToCapacityDoesntResize() {
			var buffer = new GapBuffer<int>(4);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(4, buffer.Capacity);
		}

		[TestMethod]
		public void ResizeFilledAtBeginingDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(4);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(4, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Position = 0;
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}

		[TestMethod]
		public void ResizeFilledAtEndDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(4);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(4, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}

		[TestMethod]
		public void ResizeFilledInMiddleDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(4);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(4, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Position = 2;
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}

		[TestMethod]
		public void ResizeGappedAtBeginingDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(8);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(8, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Position = 0;
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}

		[TestMethod]
		public void ResizeGappedAtEndDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(8);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(8, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}

		[TestMethod]
		public void ResizeGappedInMiddleDoesntEffectSequence() {
			var buffer = new GapBuffer<int>(8);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.AreEqual(8, buffer.Capacity);
			var sequence = buffer.ToArray();
			buffer.Position = 2;
			buffer.Capacity = 12;
			Assert.AreEqual(12, buffer.Capacity);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(sequence));
		}
	}
}
